<?php

/**

 * 开放平台基础类文件 授权及消息通知的实现

 */

namespace app\common\helper\upload;

use think\Exception;

use app\common\logic\DataSettingLogic;

use app\common\helper\upload\UploadLocalHandler;

use app\common\helper\upload\UploadQiniuHandler;

use app\common\helper\upload\UploadQcloudHandler;

use app\common\helper\upload\UploadAliHandler;

use app\common\logic\TokenLogic;

use app\common\model\ImgModel;

class  UploadHelper{

    private $setting;

    private $uploadHandler;

    private $shopId; //是店铺的记录进来

    private $type;//上传来之哪里

    public function __construct() {

        $DataSettingLogic = new DataSettingLogic();

        $this->setting = $DataSettingLogic->getData('oss');

        switch ($this->setting['type']){

            case 0:

                $this->uploadHandler = new UploadLocalHandler($this->setting);

                break;

            case 1:

                $this->uploadHandler = new UploadQiniuHandler($this->setting);

                break;

            case 2:

                $this->uploadHandler = new UploadAliHandler($this->setting);

                break;

            case 3:

                $this->uploadHandler = new UploadQcloudHandler($this->setting);

                break;

        }

    }

    

    

    public function upload(){

        $this->checkAuth();

        if(empty($_FILES['file'])){

            throw new Exception('请上传图片');

        }

        $file_name = $_FILES['file']['name'];//获取缓存区图片,格式不能变

        $type = array("jpg", "gif", 'png', 'bmp','jpeg');//允许选择的图片类型
        $ext = explode(".", $file_name);//拆分获取图片名

        $ext = strtolower($ext[count($ext) - 1]);
        if(empty($ext)){
            $ext='jpeg';
        }
        if (!in_array($ext,$type)){

            throw new Exception('图片上传不合法');

        }
        $size = filesize($_FILES['file']['tmp_name']);

        $name = md5(uniqid(true)).time().'.'.$ext;

        $res = $this->uploadHandler->upload($_FILES['file'],$name);

        $ImgModel = new ImgModel();

        $ImgModel->shop_id = $this->shopId;

        $ImgModel->hash=$res['hash'];

        $ImgModel->key=$res['key'];

        $ImgModel->from_type= $this->type;

        $ImgModel->url= $res['url'];

        $ImgModel->size= round($size/1024,2);

        $ImgModel->ext=$ext;

        $ImgModel->save();

        return  $res['url'];//仅返回图片的URL 地址

    }

    //判断一下权限

    private function checkAuth(){

       $type = request()->param('type');

       if(empty($type)){

           throw new Exception('访问接口参数不正确');

       }

       $this->type = $type;

       if($type == 'admin'){ 

           return true;

       }

       else if($type == 'miniapp'){

           $shopId = request()->param('shopId/d');

           if(empty($shopId)){

               throw new Exception('访问接口参数不正确');

           }

           $this->shopId = $shopId;

       }

       else{

           $auth_token = request()->param('auth_token');

           if(empty($auth_token)){

               throw new Exception('访问接口参数不正确');

           }

           $res = TokenLogic::getShopData($auth_token);

            if(false == $res){

                throw new Exception('访问接口参数不正确');

            }

            if(empty($res['accountId'])){

                throw new Exception('访问接口参数不正确');

            }

            if(empty($res['shopId'])){

                throw new Exception('访问接口参数不正确');

            }

            $this->shopId = $res['shopId'];

       }

    }

    

    public function __destruct() { //析构对未完成上传的文件清除

        if(!empty($_FILES)){

            foreach($_FILES as $val){

                if(!empty($val['tmp_name'])){

                    if(file_exists($val['tmp_name'])){

                        unlink($val['tmp_name']);

                    }

                }

            }

        }

    }

}